package com.test.api;

import com.google.gson.Gson;
import com.test.database.UserDao;

public class UserHandler {
	// 用户注册
	// 返回json '1'：注册成功 '0'：注册失败（用户名相同）
	public static String register(String account, String password){
		UserDao dao = UserDao.findByUserId(account);
		if (dao == null) {
			UserDao.insert(account, account, password);
			return toJson(REGISTER_SUCCESS);
		}
				
		return toJson(REGISTER_FAILED);
	}
	
	// 用户登入
	public static String login(String account, String password){
		UserDao dao = UserDao.findByUserId(account);
		if (dao != null && dao.getPassword().equals(password)){
			return toJson(LOGIN_SUCCESS);
		}
		
		return toJson(LOGIN_FAILED);
	}
	
	public static String toJson(Object src){
		Gson gson = new Gson();
		return gson.toJson(src);
	}
	
	// 响应对应值
	public static String REGISTER_SUCCESS = "REGISTER_SUCCESS";
	public static String REGISTER_FAILED = "REGISTER_FAILED";
	public static String LOGIN_SUCCESS = "LOGIN_SUCCESS";
	public static String LOGIN_FAILED = "LOGIN_FAILED";
}
